﻿#pragma once

#include "common.h"
#include "keyed.h"
#include "editor/settings.h"
#include "ui/controls.h"

namespace anip
{
	namespace editor
	{
		class CurveEditor : public EditorWindow
		{
		private:
			static PreloadedTexture
				keyAddTexture,
				keyDelTexture,
				snapTexture,
				wrapTexture;

		private:
			ui::Draggable<ui::Panel>
				* _edit = nullptr;

			ui::Button 
				* _addBtn = nullptr, 
				* _delBtn = nullptr,
				* _snapBtn = nullptr,
				* _wrapBtn = nullptr;

			Font _smallFont{};

			kfloat _value;
			struct PaintData;
			PaintData* _data = nullptr;

			void _flow();

			void _updateData();
			void _autoZoom();
			void _updateView();
			vec2 _toEditPos(invec2 pos) const;
			vec2 _fromEditPos(invec2 pos) const;
			vec2 _toEditPos(invec2 pos, invec2 zoom, invec2 offset) const;
			vec2 _fromEditPos(invec2 pos, invec2 zoom, invec2 offset) const;
			void _deletePoint(
				const kfloat::Keypoint& kp, 
				const kfloat::Keypoint* replace = nullptr);
			void _deleteSelection();

			void _scroll(invec2 pos);
			bool _beginDrag(MouseButton btn, invec2 pos);
			void _drag(invec2 start, invec2 pos);
			void _endDrag();
			void _mouseMove(invec2 pos);
			void _mouseButton(MouseButton btn, ButtonAction action);

			void _paintView();

			void _drawHGrid(float grid, bool typ, bool text);
			void _drawVGrid(float grid, bool typ, bool text);

			void _wrapMenu();

		public:
			CurveEditor(Container* parent, const kfloat& k);

			const kfloat& getValue() const { return _value; }

			void flow(ui::Flow& f) override;
		};
	}
}